package br.com.northwind.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.northwind.database.Conexao;
import br.com.northwind.exceptions.ConexaoException;
import br.com.northwind.exceptions.DriverException;
import br.com.northwind.fabricas.FabricaConexao;
import br.com.northwind.interfaces.ISuppliersDao;
import br.com.northwind.tabelas.Suppliers;

public class SuppliersDao implements ISuppliersDao {

	private final String SQL_INSERT = "INSERT INTO Suppliers (CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private final String SQL_UPDATE = "UPDATE Suppliers SET CompanyName = ?, ContactName = ?, ContactTitle = ?, Address = ?, City = ?, Region = ?, PostalCode = ?, Country = ?, Phone = ?, Fax = ?, HomePage = ? WHERE SupplierID = ?";
	private final String SQL_DELETE = "DELETE FROM Suppliers WHERE SupplierID = ?";
	private final String SQL_SELECT_TODOS = "SELECT * FROM Suppliers";
	private final String SQL_SELECT_BY_CODIGO = "SELECT * FROM Suppliers WHERE SupplierID = ?";
	
	private Conexao conexao;
	private PreparedStatement statement = null;
	private ResultSet rs = null;

	public SuppliersDao() {
		this.conexao = FabricaConexao.criarConexao();
	}

	public SuppliersDao(Conexao conexao) {
		this.conexao = conexao;
	}

	public void inserir(Suppliers _suppliers) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_INSERT);
			
			this.statement.setString(1, _suppliers.getCompanyName());
			this.statement.setString(2, _suppliers.getContactName());
			this.statement.setString(3, _suppliers.getContactTitle());
			this.statement.setString(4, _suppliers.getAddress());
			this.statement.setString(5, _suppliers.getCity());
			this.statement.setString(6, _suppliers.getRegion());
			this.statement.setString(7, _suppliers.getPostalCode());
			this.statement.setString(8, _suppliers.getCountry());
			this.statement.setString(9, _suppliers.getPhone());
			this.statement.setString(10, _suppliers.getFax());
			this.statement.setString(11, _suppliers.getHomePage());
			
			this.statement.execute();
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public void editar(Suppliers _suppliers) throws ConexaoException,DriverException {
		PreparedStatement update = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			
			this.statement.setInt(1, _suppliers.getSupplierID());
			
			this.rs = statement.executeQuery();

			if (rs.next()) {
				update = this.conexao.getConexao().prepareStatement(this.SQL_UPDATE);

				update.setString(1, _suppliers.getCompanyName());
				update.setString(2, _suppliers.getContactName());
				update.setString(3, _suppliers.getContactTitle());
				update.setString(4, _suppliers.getAddress());
				update.setString(5, _suppliers.getCity());
				update.setString(6, _suppliers.getRegion());
				update.setString(7, _suppliers.getPostalCode());
				update.setString(8, _suppliers.getCountry());
				update.setString(9, _suppliers.getPhone());
				update.setString(10, _suppliers.getFax());
				update.setString(11, _suppliers.getHomePage());

				update.execute();
				update.close();
			}
		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				if (update != null) update.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}

	}

	public void excluir(Integer _supplierid) throws ConexaoException,DriverException {
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_DELETE);
			
			this.statement.setInt(1, _supplierid);
			
			this.statement.execute();

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
				if (this.rs != null) this.rs.close();
				
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
	}

	public List<Suppliers> selecionarAll() throws ConexaoException,DriverException {
		List<Suppliers> listaDeSuppliers = new ArrayList<Suppliers>();
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_TODOS);
			this.rs = this.statement.executeQuery();

			while (rs.next()) {
				
				int SupplierID = this.rs.getInt(1);
				String CompanyName = this.rs.getString(2);
				String ContactName = this.rs.getString(3);
				String ContactTitle = this.rs.getString(4);
				String Address = this.rs.getString(5);
				String City = this.rs.getString(6);
				String Region = this.rs.getString(7);
				String PostalCode = this.rs.getString(8);
				String Country = this.rs.getString(9);
				String Phone = this.rs.getString(10);
				String Fax = this.rs.getString(11);
				String HomePage = this.rs.getString(12);
				
				listaDeSuppliers.add(new Suppliers(SupplierID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage));
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return listaDeSuppliers;
	}

	public Suppliers selecionarByCod(Integer _supplierid) throws ConexaoException, DriverException {
		Suppliers localizado = null;
		try {
			this.conexao.abrirConexao();
			this.statement = this.conexao.getConexao().prepareStatement(this.SQL_SELECT_BY_CODIGO);
			this.statement.setInt(1, _supplierid);
			this.rs = this.statement.executeQuery();

			if (rs.next()) {
				
				int SupplierID = this.rs.getInt(1);
				String CompanyName = this.rs.getString(2);
				String ContactName = this.rs.getString(3);
				String ContactTitle = this.rs.getString(4);
				String Address = this.rs.getString(5);
				String City = this.rs.getString(6);
				String Region = this.rs.getString(7);
				String PostalCode = this.rs.getString(8);
				String Country = this.rs.getString(9);
				String Phone = this.rs.getString(10);
				String Fax = this.rs.getString(11);
				String HomePage = this.rs.getString(12);
				
				localizado = new Suppliers(SupplierID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax, HomePage);
				
			}

		} catch (SQLException e) {
			throw new ConexaoException();
		} catch (ClassNotFoundException e) {
			throw new DriverException();
		} finally {
			try {
				if (this.conexao != null) this.conexao.fecharConexao();
				if (this.statement != null) this.statement.close();
			} catch (SQLException e) {
				throw new ConexaoException();
			}
		}
		return localizado;
	}

}
